﻿using DotNetty.Transport.Channels;

namespace StickyBagClient
{
    public class StickyClientHandler : SimpleChannelInboundHandler<string>
    {
        protected override void ChannelRead0(IChannelHandlerContext ctx, string msg)
        {

        }

        /// <summary>
        /// 连接上服务器的时候触发
        /// </summary>
        /// <param name="context"></param>
        public override void ChannelActive(IChannelHandlerContext context)
        {
            string data = "11111\r\n";
            //string data = "11111\n";
            
            // 不间断的发送数据100条数据
            for (int i = 0; i < 100; i++)
            {
                context.WriteAndFlushAsync(data); // 客户端向服务端发送数据
            }
        }

        /// <summary>
        /// 如果数据发送和接收过程中有异常,该方法会被触发
        /// </summary>
        /// <param name="context"></param>
        /// <param name="exception"></param>
        public override void ExceptionCaught(IChannelHandlerContext context, Exception exception)
        {
            Console.WriteLine(DateTime.Now.Millisecond);
            Console.WriteLine("{0}", exception.Message);
            context.CloseAsync();
        }
    }
}